// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyAxisAttributes.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>
#include <PyAxisTitles.h>
#include <PyAxisLabels.h>
#include <PyAxisTickMarks.h>

// ****************************************************************************
// Module: PyAxisAttributes
//
// Purpose:
//   Contains the properties for one axis.
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a AxisAttributes.
//
struct AxisAttributesObject
{
    PyObject_HEAD
    AxisAttributes *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewAxisAttributes(int);
std::string
PyAxisAttributes_ToString(const AxisAttributes *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    { // new scope
        std::string objPrefix(prefix);
        objPrefix += "title.";
        str += PyAxisTitles_ToString(&atts->GetTitle(), objPrefix.c_str(), forLogging);
    }
    { // new scope
        std::string objPrefix(prefix);
        objPrefix += "label.";
        str += PyAxisLabels_ToString(&atts->GetLabel(), objPrefix.c_str(), forLogging);
    }
    { // new scope
        std::string objPrefix(prefix);
        objPrefix += "tickMarks.";
        str += PyAxisTickMarks_ToString(&atts->GetTickMarks(), objPrefix.c_str(), forLogging);
    }
    if(atts->GetGrid())
        snprintf(tmpStr, 1000, "%sgrid = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sgrid = 0\n", prefix);
    str += tmpStr;
    return str;
}

static PyObject *
AxisAttributes_Notify(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxisAttributes_SetTitle(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;

    PyObject *newValue = NULL;
    if(!PyArg_ParseTuple(args, "O", &newValue))
        return NULL;
    if(!PyAxisTitles_Check(newValue))
        return PyErr_Format(PyExc_TypeError, "Field title can be set only with AxisTitles objects");

    obj->data->SetTitle(*PyAxisTitles_FromPyObject(newValue));

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxisAttributes_GetTitle(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;
    // Since the new object will point to data owned by this object,
    // we need to increment the reference count.
    Py_INCREF(self);

    PyObject *retval = PyAxisTitles_Wrap(&obj->data->GetTitle());
    // Set the object's parent so the reference to the parent can be decref'd
    // when the child goes out of scope.
    PyAxisTitles_SetParent(retval, self);

    return retval;
}

/*static*/ PyObject *
AxisAttributes_SetLabel(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;

    PyObject *newValue = NULL;
    if(!PyArg_ParseTuple(args, "O", &newValue))
        return NULL;
    if(!PyAxisLabels_Check(newValue))
        return PyErr_Format(PyExc_TypeError, "Field label can be set only with AxisLabels objects");

    obj->data->SetLabel(*PyAxisLabels_FromPyObject(newValue));

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxisAttributes_GetLabel(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;
    // Since the new object will point to data owned by this object,
    // we need to increment the reference count.
    Py_INCREF(self);

    PyObject *retval = PyAxisLabels_Wrap(&obj->data->GetLabel());
    // Set the object's parent so the reference to the parent can be decref'd
    // when the child goes out of scope.
    PyAxisLabels_SetParent(retval, self);

    return retval;
}

/*static*/ PyObject *
AxisAttributes_SetTickMarks(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;

    PyObject *newValue = NULL;
    if(!PyArg_ParseTuple(args, "O", &newValue))
        return NULL;
    if(!PyAxisTickMarks_Check(newValue))
        return PyErr_Format(PyExc_TypeError, "Field tickMarks can be set only with AxisTickMarks objects");

    obj->data->SetTickMarks(*PyAxisTickMarks_FromPyObject(newValue));

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxisAttributes_GetTickMarks(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;
    // Since the new object will point to data owned by this object,
    // we need to increment the reference count.
    Py_INCREF(self);

    PyObject *retval = PyAxisTickMarks_Wrap(&obj->data->GetTickMarks());
    // Set the object's parent so the reference to the parent can be decref'd
    // when the child goes out of scope.
    PyAxisTickMarks_SetParent(retval, self);

    return retval;
}

/*static*/ PyObject *
AxisAttributes_SetGrid(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the grid in the object.
    obj->data->SetGrid(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AxisAttributes_GetGrid(PyObject *self, PyObject *args)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetGrid()?1L:0L);
    return retval;
}



PyMethodDef PyAxisAttributes_methods[AXISATTRIBUTES_NMETH] = {
    {"Notify", AxisAttributes_Notify, METH_VARARGS},
    {"SetTitle", AxisAttributes_SetTitle, METH_VARARGS},
    {"GetTitle", AxisAttributes_GetTitle, METH_VARARGS},
    {"SetLabel", AxisAttributes_SetLabel, METH_VARARGS},
    {"GetLabel", AxisAttributes_GetLabel, METH_VARARGS},
    {"SetTickMarks", AxisAttributes_SetTickMarks, METH_VARARGS},
    {"GetTickMarks", AxisAttributes_GetTickMarks, METH_VARARGS},
    {"SetGrid", AxisAttributes_SetGrid, METH_VARARGS},
    {"GetGrid", AxisAttributes_GetGrid, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
AxisAttributes_dealloc(PyObject *v)
{
   AxisAttributesObject *obj = (AxisAttributesObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *AxisAttributes_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyAxisAttributes_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "title") == 0)
        return AxisAttributes_GetTitle(self, NULL);
    if(strcmp(name, "label") == 0)
        return AxisAttributes_GetLabel(self, NULL);
    if(strcmp(name, "tickMarks") == 0)
        return AxisAttributes_GetTickMarks(self, NULL);
    if(strcmp(name, "grid") == 0)
        return AxisAttributes_GetGrid(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyAxisAttributes_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyAxisAttributes_methods[i].ml_name),
                PyString_FromString(PyAxisAttributes_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyAxisAttributes_methods, self, name);
}

int
PyAxisAttributes_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "title") == 0)
        obj = AxisAttributes_SetTitle(self, args);
    else if(strcmp(name, "label") == 0)
        obj = AxisAttributes_SetLabel(self, args);
    else if(strcmp(name, "tickMarks") == 0)
        obj = AxisAttributes_SetTickMarks(self, args);
    else if(strcmp(name, "grid") == 0)
        obj = AxisAttributes_SetGrid(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
AxisAttributes_print(PyObject *v, FILE *fp, int flags)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)v;
    fprintf(fp, "%s", PyAxisAttributes_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
AxisAttributes_str(PyObject *v)
{
    AxisAttributesObject *obj = (AxisAttributesObject *)v;
    return PyString_FromString(PyAxisAttributes_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *AxisAttributes_Purpose = "Contains the properties for one axis.";
#else
static char *AxisAttributes_Purpose = "Contains the properties for one axis.";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(AxisAttributesType,         \
                  "AxisAttributes",           \
                  AxisAttributesObject,       \
                  AxisAttributes_dealloc,     \
                  AxisAttributes_print,       \
                  PyAxisAttributes_getattr,   \
                  PyAxisAttributes_setattr,   \
                  AxisAttributes_str,         \
                  AxisAttributes_Purpose,     \
                  AxisAttributes_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
AxisAttributes_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &AxisAttributesType
         || Py_TYPE(other) != &AxisAttributesType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    AxisAttributes *a = ((AxisAttributesObject *)self)->data;
    AxisAttributes *b = ((AxisAttributesObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static AxisAttributes *defaultAtts = 0;
static AxisAttributes *currentAtts = 0;

static PyObject *
NewAxisAttributes(int useCurrent)
{
    AxisAttributesObject *newObject;
    newObject = PyObject_NEW(AxisAttributesObject, &AxisAttributesType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new AxisAttributes(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new AxisAttributes(*defaultAtts);
    else
        newObject->data = new AxisAttributes;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapAxisAttributes(const AxisAttributes *attr)
{
    AxisAttributesObject *newObject;
    newObject = PyObject_NEW(AxisAttributesObject, &AxisAttributesType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (AxisAttributes *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
AxisAttributes_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewAxisAttributes(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef AxisAttributesMethods[] = {
    {"AxisAttributes", AxisAttributes_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *AxisAttributesObserver = 0;

std::string
PyAxisAttributes_GetLogString()
{
    std::string s("AxisAtts = AxisAttributes()\n");
    if(currentAtts != 0)
        s += PyAxisAttributes_ToString(currentAtts, "AxisAtts.", true);
    return s;
}

static void
PyAxisAttributes_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("AxisAtts = AxisAttributes()\n");
        s += PyAxisAttributes_ToString(currentAtts, "AxisAtts.", true);
        cb(s);
    }
}

void
PyAxisAttributes_StartUp(AxisAttributes *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyAxisAttributes_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(AxisAttributesObserver == 0)
    {
        AxisAttributesObserver = new ObserverToCallback(subj,
            PyAxisAttributes_CallLogRoutine, (void *)data);
    }

}

void
PyAxisAttributes_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete AxisAttributesObserver;
    AxisAttributesObserver = 0;
}

PyMethodDef *
PyAxisAttributes_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return AxisAttributesMethods;
}

bool
PyAxisAttributes_Check(PyObject *obj)
{
    return (obj->ob_type == &AxisAttributesType);
}

AxisAttributes *
PyAxisAttributes_FromPyObject(PyObject *obj)
{
    AxisAttributesObject *obj2 = (AxisAttributesObject *)obj;
    return obj2->data;
}

PyObject *
PyAxisAttributes_New()
{
    return NewAxisAttributes(0);
}

PyObject *
PyAxisAttributes_Wrap(const AxisAttributes *attr)
{
    return WrapAxisAttributes(attr);
}

void
PyAxisAttributes_SetParent(PyObject *obj, PyObject *parent)
{
    AxisAttributesObject *obj2 = (AxisAttributesObject *)obj;
    obj2->parent = parent;
}

void
PyAxisAttributes_SetDefaults(const AxisAttributes *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new AxisAttributes(*atts);
}

